package com.eningqu.aipen.ui;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;

import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.eningqu.aipen.demo.R;
import com.eningqu.aipen.demo.databinding.ActivityMainBinding;
import com.eningqu.aipen.ui.adapter.FragmentStateVPAdapter;
import com.eningqu.aipen.ui.base.BaseActivity;
import com.eningqu.aipen.ui.home.HomeFragment;
import com.eningqu.aipen.ui.medical.MedicalFragment;
import com.eningqu.aipen.ui.mine.MineFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.ArrayList;
import java.util.List;

/**
 * Package:com.eningqu.aipen.ui
 * Author:starr
 * Time:2023/11/7 10:42
 * Description: App主界面，承载三个主要的Fragment
 */
public class MainActivity extends BaseActivity<ActivityMainBinding> {

    private ViewPager2 mViewPager;
    private BottomNavigationView mBtnNvgView;
    private final List<Fragment> list = new ArrayList<>();

    public static void startAction(Context context) {
        Intent intent = new Intent(context, MainActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    protected void initView() {

        mViewPager = mBinding.vpMain;
        mBtnNvgView = mBinding.btnNavView;

        mViewPager.setUserInputEnabled(false);//禁止用户左右滑动
        mBtnNvgView.setOnNavigationItemSelectedListener(menuItem -> {
            switch (menuItem.getItemId()) {
                case R.id.home:
                    mViewPager.setCurrentItem(0);
                    break;
                case R.id.medical:
                    mViewPager.setCurrentItem(1);
                    break;
                case R.id.mine:
                    mViewPager.setCurrentItem(2);
                    break;
                default:
                    break;
            }
            return true;
        });
        mViewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                switch (position) {
                    case 0:
                        mBtnNvgView.setSelectedItemId(R.id.home);
                        break;
                    case 1:
                        mBtnNvgView.setSelectedItemId(R.id.medical);
                        break;
                    case 2:
                        mBtnNvgView.setSelectedItemId(R.id.mine);
                        break;
                    default:
                        break;
                }
            }
        });
    }

    @Override
    protected void initData() {
        list.add(HomeFragment.newInstance());
        list.add(MedicalFragment.newInstance());
        list.add(MineFragment.newInstance());
        FragmentStateVPAdapter mAdapter = new FragmentStateVPAdapter(this, list);
        mViewPager.setAdapter(mAdapter);

    }

    @Override
    protected void onStart() {
        super.onStart();
    }
}
